package com.zsl.swing.redis.desktop.window.panel;

import com.zsl.swing.redis.desktop.model.FontSizeConfigEntity;
import com.zsl.swing.redis.desktop.model.OtherGeneralConfigEntity;
import com.zsl.swing.redis.desktop.type.DbOptEnum;
import com.zsl.swing.redis.desktop.utils.FontUtils;
import com.zsl.swing.redis.desktop.utils.StringUtils;
import com.zsl.swing.redis.desktop.window.ZslRedisDesktopMainWindow;
import com.zsl.swing.redis.desktop.window.list.ZslKeyList;

import javax.swing.*;
import java.awt.*;

public class ZslKeyQueryPanel extends JPanel {

    private final JTextArea queryTextField = new JTextArea(5,40);

    private final JComboBox<String> actionBox = this.buildComboBox();

    private final ZslKeyList keyList;

    public ZslKeyQueryPanel(ZslDbOptPanel parent) {
        this.setLayout(new BorderLayout());

        FontSizeConfigEntity fontSizeConfig = ZslRedisDesktopMainWindow.getFontSizeConfig();
        Font font = FontUtils.getFont(fontSizeConfig.getFontName(), Font.PLAIN, fontSizeConfig.getTree());

        this.queryTextField.setLineWrap(true);
        this.queryTextField.setFont(font);
        this.queryTextField.setBorder(BorderFactory.createTitledBorder("查询条件"));

        this.keyList = new ZslKeyList(parent.getEntity(), parent.getValueShowPanel());
        this.keyList.setFont(font);

        JScrollPane keyScrollPane = new JScrollPane(this.keyList);
        keyScrollPane.setBorder(BorderFactory.createTitledBorder("keys"));

        JPanel queryPanel = new JPanel();
        queryPanel.add(new JScrollPane(this.queryTextField, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));

        JPanel optPanel = new JPanel();
        optPanel.add(this.actionBox);

        JButton runBtn = new JButton("执行(run)");
        optPanel.add(runBtn);

        JPanel northPanel = new JPanel();
        northPanel.setLayout(new BoxLayout(northPanel,BoxLayout.Y_AXIS));
        northPanel.add(queryPanel);
        northPanel.add(optPanel);

        this.add(northPanel, BorderLayout.NORTH);
        this.add(keyScrollPane, BorderLayout.CENTER);

        runBtn.addActionListener(event -> this.runBtnAction());
    }

    private void runBtnAction() {
        String actionCommand = (String)this.actionBox.getSelectedItem();
        DbOptEnum command = DbOptEnum.locate2Enum(actionCommand);

        String keyword = this.queryTextField.getText();
        if(!StringUtils.isEmpty(keyword)){
            keyword = keyword.trim();
        }

        ZslRedisDesktopMainWindow.getZslLogPanel().log("当前查询关键词：" + keyword);

        OtherGeneralConfigEntity otherGeneralConfig = ZslRedisDesktopMainWindow.getOtherGeneralConfig();
        switch (command){
            case SHOW_ALL:{
                keyword = "*";
                this.keyList.queryLikeList(keyword, null, otherGeneralConfig.getShowALLScanCount(), true);
                break;
            }
            case LIKE:{
                this.keyList.queryLikeList(keyword, null, otherGeneralConfig.getQueryLikeScanCount(), true);
                break;
            }
            case QUERY: {
                this.keyList.queryOne(keyword);
                break;
            }
            case DELETE: {
                this.keyList.deleteOne(keyword);
                break;
            }
            case DELETE_LIKE: {
                this.keyList.deleteLike(keyword, otherGeneralConfig.getDeleteLikeScanCount());
                break;
            }
            default:break;
        }
    }


    private JComboBox<String> buildComboBox(){
        JComboBox<String> actionBox = new JComboBox<>();
        actionBox.addItem(DbOptEnum.SHOW_ALL.getText());
        actionBox.addItem(DbOptEnum.LIKE.getText());
        actionBox.addItem(DbOptEnum.QUERY.getText());
        actionBox.addItem(DbOptEnum.DELETE_LIKE.getText());
        actionBox.addItem(DbOptEnum.DELETE.getText());
        return actionBox;
    }

    public ZslKeyList getKeyList(){
        return this.keyList;
    }

}
